package com.fairytask.ui
{
	import com.fairytask.data.RecordObject;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.list.List;
	import com.fairytask.engine.components.list.ListEvent;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.define.RecordDefine;
	import com.fairytask.net.CustomToClientEvent;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.ui.base.FrameUI;
	import com.fairytask.ui.item.NineEquipItem;
	
	import flash.events.MouseEvent;
	
	/**
	 * 神兵谱ui
	 * @author xujh
	 * 
	 */
	public class GetEquipUI extends FrameUI
	{
		private static var _self		:GetEquipUI;
		
		public var listTitle	:Label;
		public var normalBtn	:Button;		//普通碎片
		public var goodBtn		:Button;		//极品碎片
		private var _equipList	:List;		//装备列表
		private var _nineArr	:Array	=	[];	//九格数组
		//当前选中装备
		private var _curSelect	:int	=	0;
		//解绑用
		private var _unbind		:Array	=	[];
		//规则id
		private var _listStrArr	:Array;
		
		public static function getInstance():GetEquipUI
		{
			if (_self == null)
			{
				_self=new GetEquipUI;
			}
			return _self;
		}
		
		public function GetEquipUI(popUI:Boolean=true)
		{
			super(popUI);
			initEvent();
			initBind();
			
			//默认选中第一个
			_equipList.select(_curSelect);
			onSelect(_curSelect);
		}
		
		override public function initGUI():void
		{
			//tmp
			listTitle.htmlStr	=	"装备部位";
			normalBtn.label	=	StaticIni.getSwfLangStrVar("1000000635",[0]);
			goodBtn.label	=	StaticIni.getSwfLangStrVar("1000000636",[0]);
			
			_listStrArr	=	StaticIni.getIniList("EquipChartList");
			
			//初始化装备列表
			_equipList	=	new List(70,200,false,[],"","","center");
			initList();
			_equipList.needSelectBg = true;
			_equipList.x	=	20;
			_equipList.y	=	50;
			addChild(_equipList);
			//初始化九格
			initNine();
		}
		
		/**
		 * 初始化左侧列表 
		 * 
		 */
		private function initList():void
		{
			var item	:Object;
			for(var i:int=0;i<_listStrArr.length;i++)
			{
				item = {label:StaticIni.getSwfLangStr(_listStrArr[i]),height:20,skin:""}
				_equipList.addObj(item);
			}
		}
		
		/**
		 * 默认选中第一个装备，初始化九格 
		 * 
		 */
		private function initNine():void
		{
			var _nineEquip	:NineEquipItem;
			for(var i:int=0;i<_listStrArr.length;i++)
			{
				_nineEquip	=	new NineEquipItem();
				_nineEquip.x	=	110;
				_nineEquip.y	=	45;
				addChild(_nineEquip);
				_nineEquip.equipId	=	StaticIni.getIniVar(_listStrArr[i],"EquipList");
				_nineEquip.addEventListener(MouseEvent.CLICK,onGetEquip);
				_nineEquip.visible	=	false;
				_nineArr.push(_nineEquip);
			}
			
		}
		
		/**
		 * 添加事件 
		 * 
		 */
		private function initEvent():void
		{
			//点击列表
			_equipList.addEventListener(ListEvent.LIST_CLICK,onChangeEquip);
			
			normalBtn.addEventListener(MouseEvent.CLICK,onNormal);
			goodBtn.addEventListener(MouseEvent.CLICK,onGood);
			//碎片数量
			model.addEventListener(CustomToClientEvent.SERVER_COMPLETE_MAGIC,onPieceNum);
			//再次播放特效
			model.addEventListener(CustomToClientEvent.SERVER_COMPLETE_MAGIC_AGAIN,onLightPlay);
		}
		
		/**
		 * 再次播放特效
		 * @param evt
		 * 
		 */
		private function onLightPlay(evt:CustomToClientEvent):void
		{
			/*var pos		:int	=	evt.pList.shift()-1;
			var index	:int	=	evt.pList.shift();
			(_nineArr[index] as NineEquipItem).playToPos(pos);*/
		}
		
		/**
		 * 获取碎片数量 
		 * @param event
		 * 
		 */
		private function onPieceNum(evt:CustomToClientEvent):void
		{
		/*	normalBtn.label	=	StaticIni.getSwfLangStrVar("1000000635",[evt.pList.shift()]);
			goodBtn.label	=	StaticIni.getSwfLangStrVar("1000000636",[evt.pList.shift()]);*/
		}
		
		/**
		 * 绑定数据 
		 * 
		 */
		private function initBind():void
		{
			bindManager.BindRecord(model.scene,RecordDefine.MagSpectRec,onAddEquip,onRemoveEquip);
		}
		
		//===================绑定表格数据处理=======================
		/**
		 * 添加装备表格数据 
		 * 
		 */
		private function onAddEquip(key:String, val:RecordObject):void
		{
			//位置
			var index	:int	=	int(key);
			_unbind[index]	=	[];
			//绑定九格状态
			for(var i:int=0;i<9;i++)
			{
				_unbind[index].push([val,String(i+1),bindManager.BindRecordProp(val,String(i+1),changeStatus,[index])]);
			}
			//领取状态
			_unbind[index].push([val,"10",bindManager.BindRecordProp(val,"10",getFlag,[index])]);
		}
		
		/**
		 * 更改九格状态 
		 * @param val
		 * 
		 */
		private function changeStatus(key:String,val:int,pList:Array):void
		{
			var index	:int	=	pList[0];
			(_nineArr[index] as NineEquipItem).getPiece(int(key)-1,val);
		}
		
		/**
		 * 是否全部开启 
		 * @param key
		 * @param val
		 * @param pList
		 * 
		 */
		private function getFlag(key:String,val:int,pList:Array):void
		{
			var index	:int	=	pList[0];
			(_nineArr[index] as NineEquipItem).canGet	=	Boolean(val);
			normalBtn.enabled	=	goodBtn.enabled	=	!Boolean(val);
		}
		
		/**
		 * 移除表格数据 
		 * 
		 */
		private function onRemoveEquip(key:String, val:RecordObject):void
		{
			var index	:int	=	int(key);
			//解绑数据
			for each(var itemBind:Array in _unbind[index])
			{
				bindManager.UnbindProp(itemBind.shift(),itemBind.shift(),itemBind.shift());
			}
			_unbind[index]	=	[];
			(_nineArr[index] as NineEquipItem).clearAll();
		}
		//===================绑定表格数据处理完毕=======================
		
		//=======================鼠标事件=========================
		/**
		 * 点击更换装备 
		 * @param event
		 * 
		 */
		private function onChangeEquip(e:ListEvent):void
		{
			//原选中的隐藏
			_nineArr[_curSelect].visible	=	false;
			//新选中的
			_curSelect	=	e.item.id;
			onSelect(_curSelect);
		}
		
		/**
		 * 点击普通碎片 
		 * @param event
		 * 
		 */
		private function onNormal(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHICK_TYPE,_curSelect,0);
		}
		
		/**
		 * 点击极品碎片 
		 * @param evt
		 * 
		 */
		private function onGood(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHICK_TYPE,_curSelect,1);
		}
		
		/**
		 * 点击领取 
		 * @param evt
		 * 
		 */
		private function onGetEquip(evt:MouseEvent):void
		{
			if((evt.currentTarget as NineEquipItem).canGet)
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_EQUIP,_curSelect);
			}
		}
		//=======================鼠标事件结束=========================
		/**
		 * 选中的装备 
		 * @param index
		 * 
		 */
		private function onSelect(index:int):void
		{
			_nineArr[index].visible	=	true;
			if((_nineArr[index] as NineEquipItem).canGet)
			{
				normalBtn.enabled	=	goodBtn.enabled	=	false;
			}
			else
			{
				normalBtn.enabled	=	goodBtn.enabled	=	true;
			}
		}
		
		override protected function BeforeShow():void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_MAGIC_OPEN);
		}
	}
}